package org.kl.auditsupervision.web.fnd;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.kl.auditsupervision.dto.audit.CompletionRateStatistic;
import org.kl.auditsupervision.dto.audit.InControlTypeStatistic;
import org.kl.auditsupervision.dto.audit.ProSeverityStatistic;
import org.kl.auditsupervision.dto.audit.QueTypeStatistic;
import org.kl.auditsupervision.dto.audit.RecEffectStatistic;
import org.kl.auditsupervision.entity.fnd.AuditDefect;
import org.kl.auditsupervision.entity.fnd.AuditPro;
import org.kl.auditsupervision.entity.fnd.AuditProDtl;
import org.kl.auditsupervision.entity.fnd.AuditProNop;
import org.kl.auditsupervision.entity.fnd.Employee;
import org.kl.auditsupervision.service.fnd.AuditDefectService;
import org.kl.auditsupervision.service.fnd.AuditProDtlService;
import org.kl.auditsupervision.service.fnd.AuditProService;
import org.kl.bf.entity.basic.Role;
import org.kl.bf.entity.easyui.EasyuiUtils;
import org.kl.bf.service.ServiceException;
import org.kl.bf.service.basic.AttachmentService;
import org.kl.bf.service.basic.RoleService;
import org.kl.bf.utils.KLServlets;
import org.kl.bf.web.base.BasicController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springside.modules.web.MediaTypes;

/**
 * 
 * 审计表
 * @author LJ
 *
 */
@Controller
@RequestMapping(value = "/fnd/auditPro")
public class AuditProController extends BasicController {

	@Autowired
	private AuditProService auditProService;
	@Autowired
	private AuditProDtlService auditProDtlService;
	@Autowired
	private AttachmentService attachmentService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private AuditDefectService auditDefectService;

	@RequiresPermissions(value = "auditPro:list")
	@RequestMapping(method = RequestMethod.GET)
	public String list(Model model) {
		model.addAttribute("type", 0);
		return "fnd/auditProList";
	}
	
	@RequiresPermissions(value = "auditPro:list")
	@RequestMapping(value = "0",method = RequestMethod.GET)
	public String list0(Model model) {
		model.addAttribute("type", 0);
		return "fnd/auditProList";
	}
	
	@RequiresPermissions(value = "auditPro:list")
	@RequestMapping(value = "1",method = RequestMethod.GET)
	public String list1(Model model) {
		model.addAttribute("type", 1);
		return "fnd/auditProList";
	}
	
	@RequiresPermissions(value = "auditPro:list")
	@RequestMapping(value = "2",method = RequestMethod.GET)
	public String list2(Model model) {
		model.addAttribute("type", 2);
		return "fnd/auditProList";
	}
	
	@RequiresPermissions(value = "auditPro:list")
	@RequestMapping(value = "3",method = RequestMethod.GET)
	public String list3(Model model) {
		model.addAttribute("type", 3);
		return "fnd/auditProList";
	}
	
	
	@RequiresPermissions(value = "auditPro:list")
	@RequestMapping(value = "4",method = RequestMethod.GET)
	public String list4(Model model) {
		model.addAttribute("type", 4);
		return "fnd/auditProList";
	}
	
	@RequiresPermissions(value = "auditPro:list")
	@RequestMapping(value = "5",method = RequestMethod.GET)
	public String list5(Model model) {
		model.addAttribute("type", 5);
		return "fnd/auditProList";
	}
	
	@RequiresPermissions(value = "auditPro:list")
	@RequestMapping(value = "6",method = RequestMethod.GET)
	public String list6(Model model) {
		model.addAttribute("type", 6);
		return "fnd/auditProList";
	}
	
	@RequiresPermissions(value = "auditPro:list")
	@RequestMapping(value = "7",method = RequestMethod.GET)
	public String list7(Model model) {
		model.addAttribute("type", 7);
		return "fnd/auditProList";
	}

	@RequiresPermissions(value = "auditPro:list")
	@RequestMapping(value = "auditShowList", method = RequestMethod.GET,produces = MediaTypes.JAVASCRIPT_UTF_8)
	public String AuditShowList() {
		return "fnd/auditShowList";
	}
	
	@RequestMapping(value = "recEffectStatisticList",method = RequestMethod.GET)
	public String RecEffectStatisticList(Model model) {
		return "fnd/recEffectStatisticList";
	}
	
	@RequestMapping(value = "queTypeStatisticList",method = RequestMethod.GET)
	public String QueTypeStatisticList(Model model) {
		return "fnd/queTypeStatisticList";
	}
	
	@RequestMapping(value = "proSeverityStatisticList",method = RequestMethod.GET)
	public String ProSeverityStatisticList(Model model) {
		return "fnd/proSeverityStatisticList";
	}
	
	@RequestMapping(value = "inControlTypeStatisticList",method = RequestMethod.GET)
	public String InControlTypeStatisticList(Model model) {
		return "fnd/inControlTypeStatisticList";
	}
	
	@RequestMapping(value = "completionRateStatisticList",method = RequestMethod.GET)
	public String CompletionRateStatisticList(Model model) {
		return "fnd/completionRateStatisticList";
	}
	
	
	
	

	@RequestMapping(value = "detail")
	public String detail(@RequestParam(value = "id", defaultValue = "") String id,
			@RequestParam(value = "businessKey", defaultValue = "") String businessKey,
			@RequestParam(value = "type", defaultValue = "") String type, Model model,
			HttpServletRequest request) {
		AuditPro auditPro;
		if(StringUtils.isBlank(id)){
			auditPro=auditProService.getOne(businessKey);
		}
		else{
			auditPro=auditProService.getOne(id);
		}
		model.addAttribute("auditPro", auditPro);
		model.addAttribute("type", type);
		return "fnd/auditProDtlList";
	}
	
	

	// 获取数据
	@RequiresPermissions(value = "auditPro:list")
	@RequestMapping(value = "dataGrid", produces = { MediaTypes.TEXT_HTML_UTF_8, MediaTypes.JSON_UTF_8 })
	@ResponseBody
	public String dataGrid(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
			@RequestParam(value = "rows", defaultValue = PAGE_SIZE) int pageSize,
			@RequestParam(value = "sort", defaultValue = "createTime") String sortType,
			@RequestParam(value = "order", defaultValue = "desc") String orderType, Model model, ServletRequest request) {
		try {
			Map<String, Object> searchParams = KLServlets.getParametersStartingWith(request, SEARCH_PREFIX);
			Page<AuditPro> auditPro = auditProService.getPage(searchParams, pageNumber, pageSize, sortType,
					orderType);
			return EasyuiUtils.buildEasyuiGridJsonString(auditPro.getContent(), (int) auditPro.getTotalElements());
		} catch (Exception e) {
			e.printStackTrace();
			return ajaxMsg("删除失败:" + e.getMessage(), false, null);
		}
	}
	

	@RequiresPermissions(value = "auditPro:delete")
	@RequestMapping(value = "multiDelete", produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String multiDelete(@RequestParam(value = "ids", defaultValue = "") String ids) {
		String msg = "";
		try {
			String[] idArray = StringUtils.split(ids, ",");
			for(String id:idArray){
				if(StringUtils.isNotBlank(id)){
 				   List<AuditProDtl> list=auditProDtlService.findByAuditProId(id);//删除明细
 				   if(list.size()>0){
 					  throw new ServiceException("该项目存在明细,请核对!");
 				   }
 				       
				}
			}
			msg = auditProService.multiDelete(ids);
		} catch (Exception e) {
			e.printStackTrace();
			return ajaxMsg("删除失败:" + e.getMessage(), false, null);
		}
		return ajaxMsg("删除成功:" + msg, true, null);
	}

	// 添加与更新
	@RequiresPermissions(value = "auditPro:update")
	@RequestMapping(value = "update", method = RequestMethod.POST, produces = { MediaTypes.TEXT_HTML_UTF_8,
			MediaTypes.JSON_UTF_8 })
	@ResponseBody
	public String update(@Valid @ModelAttribute("auditPro") AuditPro auditPro, ServletRequest request) {
		try {
			entityStatusValidate(auditPro);
			if(StringUtils.isBlank(auditPro.getId())){
				List<AuditPro> auditProList=auditProService.findByAuditName(auditPro.getAuditName());
				if(!auditProList.isEmpty()){
					return ajaxMsg("保存失败,名称不能重复!", false, null);
				}
			}
			auditProService.save(auditPro);
			return ajaxMsg("保存成功!", true, null);
		} catch (Exception e) {
			e.printStackTrace();
			return ajaxMsg("保存失败!", false, null);
		}
	}
	@RequiresPermissions("auditPro:export")
	@RequestMapping(value = "excelExport", produces = MediaTypes.JSON_UTF_8)
	@ResponseBody
	public String excelExport(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
			@RequestParam(value = "sort", defaultValue = "id") String sortType,
			@RequestParam(value = "order", defaultValue = "desc") String orderType, Model model, ServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> searchParams = KLServlets.getParametersStartingWith(request, SEARCH_PREFIX);
		List<AuditPro> auditPro =auditProService.searchAll(searchParams, sortType, orderType);
		super.export(AuditPro.class, response, auditPro, "审计表");
		return null;
	}
	
	@RequestMapping(value = "recEffectStatisticExcelExport", produces = MediaTypes.JSON_UTF_8)
	@ResponseBody
	public String recEffectStatisticExcelExport(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
			@RequestParam(value = "sort", defaultValue = "empName") String sortType,
			@RequestParam(value = "order", defaultValue = "asc") String orderType, Model model, ServletRequest request,
			HttpServletResponse response) {
		String startYear=request.getParameter("startYear");
	    String endYear=request.getParameter("endYear");	
	    String type=request.getParameter("type");	
		 List<RecEffectStatistic> recEffectStatisticList = auditProService.getRecEffectStatistic(startYear,endYear,type,0,0,sortType,orderType);
		super.export(RecEffectStatistic.class, response, recEffectStatisticList, "整改有效性评价统计");
		return null;
	}
	
	@RequestMapping(value = "queTypeStatisticExcelExport", produces = MediaTypes.JSON_UTF_8)
	@ResponseBody
	public String queTypeStatisticExcelExport(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
			@RequestParam(value = "sort", defaultValue = "empName") String sortType,
			@RequestParam(value = "order", defaultValue = "asc") String orderType, Model model, ServletRequest request,
			HttpServletResponse response) {
		String startYear=request.getParameter("startYear");
	    String endYear=request.getParameter("endYear");	
	    String type=request.getParameter("type");	
	    List<QueTypeStatistic> queTypeStatisticList = auditProService.getQueTypeStatistic(startYear,endYear,type,0,0,sortType,orderType);
		
		super.export(QueTypeStatistic.class, response, queTypeStatisticList, "问题类别统计");
		return null;
	}
	
	@RequestMapping(value = "proSeverityStatisticExcelExport", produces = MediaTypes.JSON_UTF_8)
	@ResponseBody
	public String proSeverityStatisticExcelExport(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
			@RequestParam(value = "sort", defaultValue = "empName") String sortType,
			@RequestParam(value = "order", defaultValue = "asc") String orderType, Model model, ServletRequest request,
			HttpServletResponse response) {
		String startYear=request.getParameter("startYear");
	    String endYear=request.getParameter("endYear");	
	    String type=request.getParameter("type");	
		 List<ProSeverityStatistic> proSeverityStatisticList = auditProService.getProSeverityStatistic(startYear,endYear,type,0,0,sortType,orderType);
		super.export(ProSeverityStatistic.class, response, proSeverityStatisticList, "问题严重程度统计");
		return null;
	}
	
	@RequestMapping(value = "inControlTypeStatisticExcelExport", produces = MediaTypes.JSON_UTF_8)
	@ResponseBody
	public String inControlTypeStatisticExcelExport(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
			@RequestParam(value = "sort", defaultValue = "empName") String sortType,
			@RequestParam(value = "order", defaultValue = "asc") String orderType, Model model, ServletRequest request,
			HttpServletResponse response) {
		String startYear=request.getParameter("startYear");
	    String endYear=request.getParameter("endYear");	
	    String type=request.getParameter("type");	
		 List<InControlTypeStatistic> inControlTypeStatisticList = auditProService.getInControlTypeStatistic(startYear,endYear,type,0,0,sortType,orderType);
		super.export(InControlTypeStatistic.class, response, inControlTypeStatisticList, "内控类别统计");
		return null;
	}
	
	@RequestMapping(value = "completionRateStatisticExcelExport", produces = MediaTypes.JSON_UTF_8)
	@ResponseBody
	public String completionRateStatisticExcelExport(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
			@RequestParam(value = "sort", defaultValue = "empName") String sortType,
			@RequestParam(value = "order", defaultValue = "asc") String orderType, Model model, ServletRequest request,
			HttpServletResponse response) {
		String startDate=request.getParameter("startDate");
	    String endDate=request.getParameter("endDate");	
	    String type=request.getParameter("type");
	    String orgId=request.getParameter("orgId");
	    if (!isPermission("auditProDtl" + ":allQuery")) {
	    	orgId=BasicController.getCurrentUserDeptId();
		}
	    List<CompletionRateStatistic> completionRateStatisticList = auditProService.getCompletionRateStatistic(startDate,endDate,type,orgId,0,0,sortType,orderType);
		super.export(CompletionRateStatistic.class, response, completionRateStatisticList, "整改率统计");
		return null;
	}
	
	//整改有效性评价统计
	@RequestMapping(value = "recEffectStatistic" , produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String RecEffectStatisticDataGrid(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
			@RequestParam(value = "rows", defaultValue = PAGE_SIZE) int pageSize,
			@RequestParam(value = "sort", defaultValue = "empName") String sortType,
			@RequestParam(value = "order", defaultValue = "asc") String orderType, Model model, ServletRequest request) {
		try{
			String startYear=request.getParameter("startYear");
		    String endYear=request.getParameter("endYear");	
		    String type=request.getParameter("type");	    
		    List<RecEffectStatistic> recEffectStatisticDaoList = auditProService.getRecEffectStatistic(startYear,endYear,type,pageNumber,pageSize,sortType,orderType);
			int count=auditProService.getRecEffectStatisticCount(startYear,endYear,type);
			return EasyuiUtils.buildEasyuiGridJsonString(recEffectStatisticDaoList,count);
		}catch(Exception e){
			e.printStackTrace();
			return ajaxMsg("加载数据失败！", false, null);
		}
	}
	
	//问题类别统计
	@RequestMapping(value = "queTypeStatistic" , produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String QueTypeStatisticDataGrid(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
			@RequestParam(value = "rows", defaultValue = PAGE_SIZE) int pageSize,
			@RequestParam(value = "sort", defaultValue = "orgName") String sortType,
			@RequestParam(value = "order", defaultValue = "asc") String orderType, Model model, ServletRequest request) {
		try{
		    String startYear=request.getParameter("startYear");
		    String endYear=request.getParameter("endYear");	
		    String type=request.getParameter("type");	
		    List<QueTypeStatistic> queTypeStatisticList = auditProService.getQueTypeStatistic(startYear,endYear,type,pageNumber,pageSize,sortType,orderType);
			int count=auditProService.getQueTypeStatisticCount(startYear,endYear,type);
			return EasyuiUtils.buildEasyuiGridJsonString(queTypeStatisticList,count);
		}catch(Exception e){
			e.printStackTrace();
			return ajaxMsg("加载数据失败！", false, null);
		}
	}
	
	//问题严重程度统计
	@RequestMapping(value = "proSeverityStatistic" , produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String ProSeverityStatisticDataGrid(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
			@RequestParam(value = "rows", defaultValue = PAGE_SIZE) int pageSize,
			@RequestParam(value = "sort", defaultValue = "orgName") String sortType,
			@RequestParam(value = "order", defaultValue = "asc") String orderType, Model model, ServletRequest request) {
		try{
			String startYear=request.getParameter("startYear");
		    String endYear=request.getParameter("endYear");	
		    String type=request.getParameter("type");		    
		    List<ProSeverityStatistic> proSeverityStatisticList = auditProService.getProSeverityStatistic(startYear,endYear,type,pageNumber,pageSize,sortType,orderType);
			int count=auditProService.getProSeverityStatisticCount(startYear,endYear,type);
			return EasyuiUtils.buildEasyuiGridJsonString(proSeverityStatisticList,count);
		}catch(Exception e){
			e.printStackTrace();
			return ajaxMsg("加载数据失败！", false, null);
		}
	}
	
	//内控类别统计
	@RequestMapping(value = "inControlTypeStatistic" , produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String InControlTypeStatisticDataGrid(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
			@RequestParam(value = "rows", defaultValue = PAGE_SIZE) int pageSize,
			@RequestParam(value = "sort", defaultValue = "empName") String sortType,
			@RequestParam(value = "order", defaultValue = "asc") String orderType, Model model, ServletRequest request) {
		try{
			String startYear=request.getParameter("startYear");
		    String endYear=request.getParameter("endYear");	
		    String type=request.getParameter("type");	   
		    List<InControlTypeStatistic> inControlTypeStatisticList = auditProService.getInControlTypeStatistic(startYear,endYear,type,pageNumber,pageSize,sortType,orderType);
			int count=auditProService.getInControlTypeStatisticCount(startYear,endYear,type);
			return EasyuiUtils.buildEasyuiGridJsonString(inControlTypeStatisticList,count);
		}catch(Exception e){
			e.printStackTrace();
			return ajaxMsg("加载数据失败！", false, null);
		}
	}
	
	//整改率统计
		@RequestMapping(value = "completionRateStatistic" , produces = MediaTypes.JAVASCRIPT_UTF_8)
		@ResponseBody
		public String CompletionRateStatisticDataGrid(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
				@RequestParam(value = "rows", defaultValue = PAGE_SIZE) int pageSize,
				@RequestParam(value = "sort", defaultValue = "empName") String sortType,
				@RequestParam(value = "order", defaultValue = "asc") String orderType, Model model, ServletRequest request) {
			try{
				String startDate=request.getParameter("startDate");
			    String endDate=request.getParameter("endDate");	
			    String type=request.getParameter("type");
			    String orgId=request.getParameter("orgId");
			    if (!isPermission("auditProDtl" + ":allQuery")) {
			    	orgId=BasicController.getCurrentUserDeptId();
				}
			    List<CompletionRateStatistic> completionRateStatisticList = auditProService.getCompletionRateStatistic(startDate,endDate,type,orgId,pageNumber,pageSize,sortType,orderType);
				int count=auditProService.getCompletionRateStatisticCount(startDate,endDate,type,orgId);
				return EasyuiUtils.buildEasyuiGridJsonString(completionRateStatisticList,count);
			}catch(Exception e){
				e.printStackTrace();
				return ajaxMsg("加载数据失败！", false, null);
			}
		}
		 
	 
	/**
	 * 所有RequestMapping方法调用前的Model准备方法, 实现Struts2
	 * Preparable二次部分绑定的效果,先根据form的id从数据库查出Task对象,再把Form提交的内容绑定到该对象上。
	 * 因为仅update()方法的form中有id属性，因此仅在update时实际执行.
	 */
	@ModelAttribute
	public void init(@RequestParam(value = "id", defaultValue = "-1") String id, Model model) {
		if (!id.equals("-1")) {
			model.addAttribute("auditPro", auditProService.getOne(id));
		}
	}
}
